<?php
    class Admincp_Model_Specialreservation extends Zend_Db_Table_Abstract
    {
        protected $_name = 'special_reservation';
        protected $_primary = 'id';
        protected $_db;
        
        public function __construct()
        {
            $this->_db = Zend_Registry::get('db');
        }
        
        public function listItem()
        {
            $data = array(
                'id',
                'hotel',
                'fullname',
                'phone',
                'create_date',
                'type',
                'status',
            );
            $sql = $this->_db->select()
                            ->from($this->_name, $data)
                            ->order('id DESC');
            return $sql;
        }
        
        public function saveItem($data, $option = null)
        {
            if($option == 'create') {
                $this->_db->insert($this->_name, $data);
            }
            else if($option == 'update') {
                $where = $this->getAdapter()->quoteInto('id = ?', $data['id']);
                $this->_db->update($this->_name, $data, $where);
            }
        }
        
        public function deleteItem($id)
        {
            $where = $this->getAdapter()->quoteInto('id = ?', $id);
            $this->_db->delete($this->_name, $where);
        }
        
        public function deleteMultiItem($listId)
        {
            foreach($listId as $id) {
                $this->deleteItem($id);
            }
        }
        
        public function getItem($id)
        {
            $data = array(
                'hotel',
                'start_date',
                'finish_date',
                'qty_room',
                'qty_adult',
                'qty_child',
                'add_bed',
                'requirement',
                'fullname',
                'phone',
                'address',
                'email',
                'yahoo',
                'skype',
                'create_date',
                'type',
            );
            $sql = $this->_db->select()
                            ->from($this->_name, $data)
                            ->where('id = ?', $id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function getType($type)
        {
            switch($type) {
                case    1   :   $result = 'Đặt phòng'; break;
                case    2   :   $result = 'Đổi thông tin'; break;
                case    3   :   $result = 'Hủy phòng'; break;
            }
            return $result;
        }
        
        public function search($data)
        {
            $arr = array(
                'id',
                'hotel',
                'fullname',
                'phone',
                'create_date',
                'type',
                'status',
            );
            $sql = $this->_db->select()
		                    ->from($this->_name, $arr);
		    if($data['hotel'] != '%%') {
		        $sql = $sql->where('hotel_plaintext LIKE ?', $data['hotel']);
		    }
            if($data['fullname'] != '%%') {
		        $sql = $sql->where('fullname_plaintext LIKE ?', $data['fullname']);
		    }
            if($data['phone'] != '%%') {
		        $sql = $sql->where('phone = ?', $data['phone']);
		    }
		    if($data['type'] != 0) {
		        $sql = $sql->where('type = ?', $data['type']);
		    }
            $sql->order('fullname ASC');
		    return $sql;
        }
    }
